<template>
  <div>
    <h1>用户列表</h1>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @change="getUserList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUserList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="dialogVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="userList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              @change="userStatus(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="权限" prop="role"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              content="编辑用户"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="openUpdateUser(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              content="删除用户"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                v-on:click="openDeleteUser(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加用户对话框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 内容主题区域 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 修改用户对话框 -->
    <el-dialog
      title="修改用户"
      :visible.sync="updateUserDialogVisible"
      width="50%"
      @close="updateDialogClosed"
    >
      <!-- 内容主题区域    :rules="addFormRules"      :model="updateUserData" -->
      <el-form ref="updateUserFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="updateUserData.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="updateUserData.password"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="updateUserData.role"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateUserDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateUser">修 改</el-button>
      </span>
    </el-dialog>

    <!-- 删除用户对话框 -->
    <el-dialog
      title="删除用户"
      :visible.sync="deleteUserDialogVisible"
      width="30%"
    >
      <!-- 内容主题区域 -->
      <h2>你确定要删除用户</h2>
      <el-form
        :model="deleteUserData"
        ref="updateUserFormRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="deleteUserData.username"></el-input>
        </el-form-item>
      </el-form>

      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteUserDialogVisible = false">取 消</el-button>
        <el-button type="danger" @click="deleteUser">删 除</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pageNumber: 1,
        pageSize: 5
      },
      userList: [],
      total: 0,
      dialogVisible: false,
      updateUserDialogVisible: false,
      deleteUserDialogVisible: false,
      addForm: {
        username: "",
        password: ""
      },
      deleteUserData: [],
      updateUserData: [],
      addFormRules: {
        username: [
          { required: true, message: "请输入用户名", tirgger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名的长度在3~10个字符",
            tirgger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入密码", tirgger: "blur" },
          { min: 3, max: 10, message: "密码长度在3~10个字符", tirgger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    getUserList() {
      this.$http({
        method: "get",
        params: {
          username: this.queryInfo.query
        },
        url: "user/" + this.queryInfo.pageSize + "/" + this.queryInfo.pageNumber
      })
        .then(response => {
          if (response.data.code === 0) {
            this.$message.info("获取用户列表成功");
            this.userList = response.data.data.items;
            this.total = response.data.data.total;
          }
          console.log(response.data);
        })
        .catch(error => {
          console.log(error);
        });
    },
    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryInfo.pageSize = newSize;
      this.getUserList();
    },
    handleCurrentChange(newPage) {
      console.log(newPage);
      this.queryInfo.pageNumber = newPage;
      this.getUserList();
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    updateDialogClosed() {
      this.$refs.updateUserFormRef.resetFields();
    },
    openUpdateUser(rowInfo) {
      console.log(rowInfo);
      this.updateUserData = rowInfo;
      this.updateUserDialogVisible = true;
      console.log(rowInfo);
    },
    updateUser() {
      this.updateUserDialogVisible = false;
      this.$http({
        method: "put",
        url: "user/",
        data: this.updateUserData
      });
      console.log(this.updateUserData);
    },
    openDeleteUser(userInfo) {
      console.log(userInfo);
      this.deleteUserData = userInfo;
      this.deleteUserDialogVisible = true;
    },
    deleteUser() {
      this.deleteUserDialogVisible = false;
      this.$http.delete("user/" + this.deleteUserData.id).then(response => {
        console.log(response);
        this.$message.success("删除成功");
      });
    },
    userStatus(userInfo) {
      console.log(userInfo);
      this.$http
        .put("user/" + userInfo.id + "/status/" + userInfo.status)
        .then(response => {
          console.log(response);
          this.$message.success("更改成功");
        });
    }
  }
};
</script>
